package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.MemberDTAdapter;
import com.store.app.bean.MembersDTBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ben
 * on 2016/8/29 0029.\
 */
//会员列表页面
public class MembersDTActivity extends BaseActivity implements IOnHttpListener {
    private ListView mListView;
    private NetRequest netRequest;
    private MemberDTAdapter memberDTAdapter;
    private List<MembersDTBean> dtBeen = new ArrayList<>();
    private TextView public_tv_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members_send_coupon_dta);
        initView();

        netRequest = new NetRequest(this);
        mListView = (ListView) findViewById(R.id.lv_members);
        netRequest.requestRegistMemeberList(1, MainActivity.member_id);
    }

    private void initView() {
        ((TextView) findViewById(R.id.tvTitle)).setText("会员列表");
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        public_tv_right = (TextView) findViewById(R.id.public_tv_right);
        public_tv_right.setVisibility(View.VISIBLE);
        public_tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MembersDTActivity.this,AddContactsActivity.class);
                intent.putExtra("type",0);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onAfinalFail() {

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        //加载更多
        if (requestId == 1) {
            try {
                JSONObject data = new JSONObject(result);
                Log.v("zyl", "data1:" + data.toString());
                Gson gson = new Gson();

                JSONArray listArr = data.getJSONArray("list");
                List<MembersDTBean> membersDTBeans = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    MembersDTBean registDetailBean = gson.fromJson(listArr.getJSONObject(i).toString(), MembersDTBean.class);
                    membersDTBeans.add(registDetailBean);
                }
//                int page_no = data.getJSONObject("page").getInt("page_no");
//                mListView.onLoadComplete();
//                if (page_no == pageNum) {
//                    mListView.setResultSize(membersDTBeans.size());
                dtBeen.addAll(membersDTBeans);
                Log.v("zyl", "eeeeeeee" + dtBeen.size());
                memberDTAdapter = new MemberDTAdapter(dtBeen, this);
                mListView.setAdapter(memberDTAdapter);
//                    pageNum += 1;
//                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
//        else if (requestId == 2) {
//            try {
//                JSONObject data = new JSONObject(result);
//                Log.v("zyl", "data1:" + data.toString());
//                Gson gson = new Gson();
//
//                JSONArray listArr = data.getJSONArray("list");
//                List<MembersDTBean> membersDTBeans = new ArrayList<>();
//                for (int i = 0; i < listArr.length(); i++) {
//                    MembersDTBean registDetailBean = gson.fromJson(listArr.getJSONObject(i).toString(), MembersDTBean.class);
//                    membersDTBeans.add(registDetailBean);
//                }
//                mListView.onRefreshComplete();
//                mListView.setResultSize(membersDTBeans.size());
//                dtBeen.clear();
//                dtBeen.addAll(membersDTBeans);
//                memberDTAdapter.notifyDataSetChanged();
//                pageNum += 1;
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
//        mListView.onRefreshComplete();
//        mListView.onLoadComplete();
        ToastUtils.showTextToast(MembersDTActivity.this, msg);
    }
}
